package role

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
)

type roleRepository struct {
}

// NewRoleRepository 构造函数
func NewRoleRepository() RoleRepositroyIface {
	return &roleRepository{}
}

// RoleSave 新增/修改角色
func (r *roleRepository) RoleSave(ctx context.Context, param *base.RoleItem) (int64, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return 0, err
	}
	defer func() {
		_ = conn.Close()
	}()

	req := &pb.RoleSaveRequest{}
	req.Param = &pb.RoleItem{
		Id:       param.Id,
		RoleName: param.Name,
	}

	client := pb.NewBaseRoleServiceClient(conn)
	resp, err := client.RoleSave(ctx, req)
	if err != nil {
		return 0, err
	}
	if resp == nil {
		return 0, nil
	}
	return resp.GetId(), nil
}

// RoleDelete 删除角色
func (r *roleRepository) RoleDelete(ctx context.Context, param *base.RoleParam) (int64, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return 0, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseRoleServiceClient(conn)
	resp, err := client.DeleteRole(ctx, &pb.DeleteRoleRequest{Id: param.RoleId})
	if err != nil {
		return 0, err
	}
	if resp == nil {
		return 0, nil
	}

	return resp.GetId(), nil
}

// RoleList 角色列表
func (r *roleRepository) RoleList(ctx context.Context) ([]*base.RoleItem, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseRoleServiceClient(conn)
	resp, err := client.RoleList(ctx, &pb.RoleListRequest{})
	if err != nil {
		return nil, err
	}

	roleList := make([]*base.RoleItem, 0)
	if resp == nil {
		return roleList, nil
	}
	for _, item := range resp.List {
		roleList = append(roleList, &base.RoleItem{
			Id:   item.Id,
			Name: item.RoleName,
		})
	}

	return roleList, nil
}

// PermissionList 根据角色获取权限列表
func (r *roleRepository) PermissionList(ctx context.Context, param *base.RoleParam) ([]*base.PermissionItem, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseRoleServiceClient(conn)
	resp, err := client.RolePermissionList(ctx, &pb.RolePermissionListRequest{RoleId: param.RoleId})
	if err != nil {
		return nil, err
	}

	permissionList := make([]*base.PermissionItem, 0)
	if resp == nil {
		return permissionList, nil
	}
	for _, item := range resp.List {
		permissionList = append(permissionList, &base.PermissionItem{
			Id:     item.Id,
			Name:   item.Name,
			Type:   item.Type,
			MenuId: item.MenuId,
		})
	}

	return permissionList, nil
}

// AddPermissionToRole 增加权限到角色
func (r *roleRepository) AddPermissionToRole(ctx context.Context, roleId int64, perIds []int64) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseRoleServiceClient(conn)
	resp, err := client.AddPermissionsToRole(ctx, &pb.AddPermissionsToRoleRequest{
		RoleId: roleId,
		PerIds: perIds,
	})
	if err != nil {
		return err
	}

	if resp == nil || resp.IsSuccess == false {
		return errors.New("增加权限失败")
	}

	return nil
}
